using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace CSharpPro._1028;

public class UserService : IUserService
{
    private readonly List<User> _users = new List<User>();

    public bool Login(string username, string password)
    {
      
        foreach (var user in _users)
        {
            if (user.Username == username && user.Password == password)
            {
                return true;
            }
        }
        return false;
    }
    public bool Register(string username, string password, string confirmPassword, out string errorMsg)
    {
        
        if (!Regex.IsMatch(username, @"^[a-zA-Z0-9_]+$"))
        {
            errorMsg = "用户名只能包含字母、数字、下划线";
            return false;
        }

        
        if (!Regex.IsMatch(password, @"^(?=.*[A-Z])(?=.*[a-z])(?=.*\d).+$"))
        {
            errorMsg = "密码必须包含至少1个大写字母、1个小写字母和1个数字";
            return false;
        }

       
        if (password != confirmPassword)
        {
            errorMsg = "密码与确认密码不一致";
            return false;
        }
        
        foreach (var user in _users)
        {
            if (user.Username == username)
            {
                errorMsg = "用户名已存在";
                return false;
            }
        }

       
        _users.Add(new User(username, password));
        errorMsg = "注册成功";
        return true;
    }


    public void Run()
    {
        while (true)
        {
            Console.WriteLine("\n主菜单：");
            Console.WriteLine("1. 登录");
            Console.WriteLine("2. 注册");
            Console.WriteLine("3. 退出");
            Console.Write("请选择功能（输入数字）：");

            var choice = Console.ReadLine();
            if (!int.TryParse(choice, out int menuChoice) || menuChoice < 1 || menuChoice > 3)
            {
                Console.WriteLine("请输入有效数字（1-3）");
                continue;
            }

            switch (menuChoice)
            {
                case 1:
                    HandleLogin();
                    break;
                case 2:
                    HandleRegister();
                    break;
                case 3:
                    Console.WriteLine("退出程序");
                    return;
            }
        }
    }
    
    private void HandleLogin()
    {
        Console.WriteLine("\n=== 登录 ===");
        Console.Write("用户名：");
        var username = Console.ReadLine();
        Console.Write("密码：");
        var password = Console.ReadLine();

        if (Login(username, password))
        {
            Console.WriteLine("登录成功！进入系统首页（模拟）");
        }
        else
        {
            Console.WriteLine("登录失败，用户名不存在或密码错误");
        }
    }


    private void HandleRegister()
    {
        Console.WriteLine("\n=== 注册 ===");
        Console.Write("用户名（3-15个字符，只能包含字母、数字、下划线）：");
        var username = Console.ReadLine();
        Console.Write("密码（6-20个字符，必须包含大小写字母和数字）：");
        var password = Console.ReadLine();
        Console.Write("确认密码：");
        var confirmPassword = Console.ReadLine();

        if (Register(username, password, confirmPassword, out var errorMsg))
        {
            Console.WriteLine(errorMsg);
        }
        else
        {
            Console.WriteLine($"注册失败：{errorMsg}");
        }
    }
}